package com.mcxx.modules.common.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.common.service.impl.IntegrateCheckServiceImpl;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.param.CheckListSearchParam;
import com.mcxx.modules.miniSalvation.check.service.read.CheckReadService;
import com.mcxx.modules.miniSalvation.check.service.write.CheckWriteService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Validated
@Api("核对一体化控制类")
@RestService({"integrate/check"})
public class IntegrateController extends DefaultRestService {
    @Autowired
    private CheckReadService checkReadService;
    @Autowired
    private CheckWriteService checkWriteService;
    @Autowired
    private IntegrateCheckServiceImpl integrateCheckService;

    private static final Logger logger = LoggerFactory.getLogger(IntegrateController.class);

    @PostMapping("queryCheckFile")
    @ApiOperation("查询核对一体化报告")
    public void queryCheckFile(@V({"notnull"}) String checkId){
        logger.info("查询核对一体化核对报告：{}",checkId);
        CheckEntity entity = checkReadService.selectById(checkId);
        if(CommonUtil.invalidArgs(entity) || !Constant.IntegrateCheckStatus.WAIT_SAVE.equals(entity.getCheckState())){
            setData("数据错误");
        }
        String res = integrateCheckService.queryCheckFile(entity.getCheckId(), entity.getOrderId(),getCurUserAccount());
        setData(res);
        logger.info("查询核对一体化核对报告完成：{}",res);
    }


    @PostMapping("uploadCheckFile")
    @ApiOperation("保存核对文件")
    public void uploadIntegrateCheckFile(@RequestParam("file") MultipartFile[] file, String checkId){
        logger.info("保存核对文件,参数是{}",checkId);
        int rst = integrateCheckService.saveIntegratedCheckFile(file,checkId);
        logger.info("保存核对文件,结果是：{}",rst);
    }



    @PostMapping("getIntegratList")
    @ApiOperation("一体化区县查询列表")
    public void getIntegratList(CheckListSearchParam param, PageParam pageParam){
        logger.info("一体化区县查询列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = checkReadService.selectIntegrateCheckList(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("一体化区县查询列表,结果：{}", JSON.toJSONString(page));
    }

    @PostMapping("downIntegratList")
    @ApiOperation(value="导出一体化查询列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出一体化列表")
    public void downIntegratList(HttpServletResponse response, @RequestBody CheckListSearchParam param)throws IOException {
        logger.info("开始导出一体化查询列表，参数是：{}", param);
        ExcelInfo excelInfo = checkWriteService.downLoadIntegrateCheckList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("开始导出一体化查询列表，查询结果是：{}", excelInfo);
    }


    @PostMapping("conferIntegratList")
    @ApiOperation(value="一体化核对报告下发")
    public void conferIntegrat(@RequestParam(value="checkIds") List<String> checkIds) {
        logger.info("开始一体化核对报告下发，参数是：{}", checkIds);
        String msg = checkWriteService.conferIntegrat(checkIds);
        setData(msg);
        logger.info("开始一体化核对报告下发结束，查询结果是：{}", msg);
    }

    @PostMapping("listCheckRecord")
    @ApiOperation(value="查询核对记录")
    public void listCheckRecord(CheckListSearchParam param, PageParam pageParam) {
        logger.info("开始查询核对记录，参数是：{}", param);
        Page page = checkReadService.selectCheckRecordByParam(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询核对记录结束");
    }
}
